Hands-On exercise to practice ggiraph, plotlyr and gganimate
packages = c('tidyverse','readxl','ggiraph','plotly','gganimate','DT','patchwork','gifski','gapminder')
for(p in packages){library
if(!require(p, character.only = T)){
install.packages(p)
}
library(p,character.only = T)
}
Loading the CSV file
exam_data <- read_csv("data/Exam_data.csv")
p <- ggplot(data = exam_data,
aes(x = MATHS)) +
geom_dotplot_interactive(
aes(tooltip = ID) ,
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
scale_y_continuous(NULL,
breaks = NULL)
girafe(
ggobj = p,
width_svg = 6,
height_svg = 6*0.618
)
plot with class dataid and patchwork for linking w eng graph
p1 <- ggplot(data = exam_data,
aes(x = MATHS)) +
geom_dotplot_interactive(
aes(data_id = CLASS,
tooltip = ID),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
coord_cartesian(xlim=c(0,100)) +
scale_y_continuous(NULL,
breaks = NULL)
girafe(
ggobj = p,
width_svg = 6,
height_svg = 6*0.618,
options = list(
opts_hover(css = "fill: #202020;"),
opts_hover_inv(css = "opacity: 0.2;")
)
)
p2 <- ggplot(data = exam_data,
aes(x = ENGLISH)) +
geom_dotplot_interactive(
aes(data_id = CLASS,
tooltip = ID),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
coord_cartesian(xlim=c(0,100)) +
scale_y_continuous(NULL,
breaks = NULL)
girafe(code = print(p1 / p2),
width_svg = 6,
height_svg = 6*0.618,
options = list(
opts_hover(css = "fill: #202020;"),
opts_hover_inv(css = "opacity: 0.2;")
)
)
plot_ly(data = exam_data,
x = ~MATHS,
y = ~ENGLISH,
color = ~RACE,
colors = "Set1")
creating a palette
using ggplotly
d <- highlight_key(exam_data)
p1 <- ggplot(data = d,
aes( x = MATHS, y = ENGLISH)) +
geom_point(dotsize = 1) +
coord_cartesian(xlim = c(0,100),
ylim = c(0,100))
p2 <- ggplot(data = d,
aes(x = MATHS,y = SCIENCE)) +
geom_point(dotsize = 1) +
coord_cartesian(xlim = c(0,100),
ylim = c(0,100))
subplot(ggplotly(p1),
ggplotly(p2))
DT::datatable(exam_data)
d <- highlight_key(exam_data)
p <- ggplot(data = exam_data,
aes( x = MATHS, y = ENGLISH)) +
geom_point(dotsize = 1) +
coord_cartesian(xlim = c(0,100),
ylim = c(0,100))
gg <- highlight(ggplotly(p),
"plotly_selected")
crosstalk::bscols(gg,
DT::datatable(d),
width = 5)